﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Text;

namespace BLL
{
    public class CommonList<T> : IList<T>
    {
        protected IList<T> mLista = new List<T>();

        #region Implementation of IEnumerable

        public IEnumerator<T> GetEnumerator()
        {
            return mLista.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        #region Implementation of ICollection<T>

        public void Add(T item)
        {
            mLista.Add(item);
        }

        public void Clear()
        {
            mLista.Clear();
        }

        public bool Contains(T item)
        {
            return mLista.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            mLista.CopyTo(array, arrayIndex);
        }

        public bool Remove(T item)
        {
            return mLista.Remove(item);
        }

        public int Count
        {
            get { return mLista.Count; }
        }

        public bool IsReadOnly
        {
            get { return mLista.IsReadOnly; }
        }

        #endregion

        #region Implementation of IList<T>

        public int IndexOf(T item)
        {
            return mLista.IndexOf(item);
        }

        public void Insert(int index, T item)
        {
            mLista.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            mLista.RemoveAt(index);
        }

        public T this[int index]
        {
            get { return mLista[index]; }
            set { mLista[index] = value; }
        }

        #endregion

        #region Your Added Stuff

        // Add new features to your collection.

        #endregion

        [EditorBrowsable(EditorBrowsableState.Never)]
        public string mMensaje;

        [EditorBrowsable(EditorBrowsableState.Never)]
        private static string mConnDB;

        public string Mensaje
        {
            get { return mMensaje; }
            set { mMensaje = value; }
        }

        protected static string ConnDB
        {
            get
            {
                if (string.IsNullOrEmpty(mConnDB))
                {
                    var tmpConexionUsada = ConfigurationManager.AppSettings["ConexionUsada"];
                    if (!string.IsNullOrEmpty(ConfigurationManager.ConnectionStrings[tmpConexionUsada].ConnectionString))
                        mConnDB = ConfigurationManager.ConnectionStrings[tmpConexionUsada].ConnectionString;

                }

                return mConnDB;
            }
        }

        protected string Encrypt(string valor)
        {
            if (valor == null) return null;

            try
            {
                byte[] encData_byte;
                encData_byte = Encoding.UTF8.GetBytes(valor);
                string encodedData = Convert.ToBase64String(encData_byte);

                return encodedData;
            }
            catch (Exception ex)
            {
                throw new Exception("Error Encrypt: " + ex.Message);
            }
        }

        protected string Decrypt(string valorEncriptado)
        {
            if (valorEncriptado == null) return null;

            var encoder = new UTF8Encoding();
            var utf8Decode = encoder.GetDecoder();
            var todecode_byte = Convert.FromBase64String(valorEncriptado);
            var charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
            var decoded_char = new char[charCount];
            utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
            var result = new String(decoded_char);
            return result;
        }
    }
}

